package com.liyuxiang.yygh.user.api;

import com.liyuxiang.yygh.common.result.Result;
import com.liyuxiang.yygh.common.utils.AuthContextHolder;
import com.liyuxiang.yygh.model.user.Patient;
import com.liyuxiang.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {
    @Autowired
    PatientService patientService;

    //获取就诊人信息
    @GetMapping("auth/findAll")
    public Result finAll(HttpServletRequest request) {
        List<Patient> list = patientService.finAllByUserId(AuthContextHolder.getUserId(request));
        return Result.ok(list);

    }

    //添加就诊人
    @PostMapping("auth/save")
    public Result savePatient(HttpServletRequest request, @RequestBody Patient patient) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        boolean flag = patientService.save(patient);
        return flag ? Result.ok() : Result.fail();
    }

    //根据id获取就诊人信息
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable("id") Long id) {
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    //修改就诊人
    @PutMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        boolean flag = patientService.updateById(patient);
        return flag ? Result.ok() : Result.fail();
    }

    //删除就诊人信息
    @DeleteMapping("auth/remove/{id}")
    public Result updatePatient(@PathVariable("id") Long id) {
        boolean flag = patientService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    //根据就诊人id获取就诊人信息
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id) {
        return patientService.getPatientById(id);
    }

}
